var async = require('async')

module.exports = function(app) {
  return new Handler(app)
}

var Handler = function(app) {
  this.app = app
  Handler.app = app
  this.sessionService = this.app.get('sessionService')
  this.channelService = this.app.get('channelService')
  Handler.robotTimer = 0
  Handler.robotFlag = false
  Handler.robotInfo = {
  	"table" : [0,0,0,0,0,0,0,0,0,0],
  	"time" : 5000,
  }
}

var handler = Handler.prototype
var local = {}

//获取签到奖励
handler.getLoginAward = function(msg,session,next) {
	var self = this
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	self.app.rpc.db.award.getLoginAward(session,uid,function(flag,data){
		next(null,{flag : flag,data : data})
	})
}
//获取签到信息
handler.getLoginInfo = function(msg,session,next) {
	var self = this
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	self.app.rpc.db.award.getLoginInfo(session,uid,function(flag,data){
		var info = {
			curTime : (new Date()).getTime(),
			info : data
		}
		next(null,{flag : flag,info})
	})
}
//设置自动机器人状态
handler.setRobotState = function(msg,session,next) {
	var self = this
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	async.waterfall([
		function(cb) {
			//权限验证
			self.app.rpc.db.remote.getValue(null,uid,"limits",function(data) {
				if(data >= 5){
					cb()
				}else{
					next(null,{flag : false})
				}
			})
		},
		function() {
			if(msg.state === true){
				Handler.robotFlag = true
				Handler.robotInfo.table = msg.table
				Handler.robotInfo.time = msg.time
				if(typeof(Handler.robotInfo.time) !== "number" || Handler.robotInfo.time < 5000){
					Handler.robotInfo.time = 5000
				}
				local.beginRobot()
			}else{
				Handler.robotFlag = false
			}
		}
		],function(err,data) {
		console.log(err)
		console.log(data)
	})
	next(null,{flag : true})
}
//开启机器人自动抽奖
local.beginRobot = function() {
	clearTimeout(Handler.robotTimer)
	if(Handler.robotFlag == false){
		return
	}
	var index = local.getRandomIndex(Handler.robotInfo.table)
	Handler.app.rpc.db.award.addRobotLottery(null,index,function(data){
	})
	var randomTime = Handler.robotInfo.time *  (Math.random() * 1.5 + 0.5)
	Handler.robotTimer = setTimeout(local.beginRobot,randomTime)
}
//设置库存表
handler.setLotteryInventoryItem = function(msg,session,next) {
	var self = this
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	async.waterfall([
		function(cb) {
			//权限验证
			self.app.rpc.db.remote.getValue(null,uid,"limits",function(data) {
				if(data >= 5){
					cb()
				}else{
					next(null,{flag : false})
				}
			})
		},
		function() {
			self.app.rpc.db.award.setLotteryInventoryItem(session,msg.data,function(data){
				next(null,{data : data})
			})
		}
		],function(err,data) {
		console.log(err)
		console.log(data)
	})
}
//设置转盘表
handler.setLotteryTable = function(msg,session,next) {
	var self = this
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	async.waterfall([
		function(cb) {
			//权限验证
			self.app.rpc.db.remote.getValue(null,uid,"limits",function(data) {
				if(data >= 5){
					cb()
				}else{
					next(null,{flag : false})
				}
			})
		},
		function() {
			self.app.rpc.db.award.setLotteryTable(session,msg.data,function(data){
				next(null,{data : data})
			})
		}
		],function(err,data) {
		console.log(err)
		console.log(data)
	})
}
//获取抽奖次数列表
handler.getLotteryCountList = function(msg,session,next) {
	var self = this
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	async.waterfall([
		function(cb) {
			//权限验证
			self.app.rpc.db.remote.getValue(null,uid,"limits",function(data) {
				if(data >= 5){
					cb()
				}else{
					next(null,{flag : false})
				}
			})
		},
		function() {
			self.app.rpc.db.award.getLotteryCountList(session,function(data){
				next(null,{data : data})
			})
		}
		],function(err,data) {
		console.log(err)
		console.log(data)
	})
}
//设置奖品状态,0 : 未发送   10:已发送完成
handler.sendAwardState = function(msg,session,next) {
	var self = this
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	var targetUid = msg.uid
	var orderNumber = msg.orderNumber
	var state = msg.state
	async.waterfall([
		function(cb) {
			//权限验证
			self.app.rpc.db.remote.getValue(null,uid,"limits",function(data) {
				if(data >= 5){
					cb()
				}else{
					next(null,{flag : false})
				}
			})
		},
		function() {
			self.app.rpc.db.award.sendAwardState(session,targetUid,orderNumber,state,function(data){
				next(null,{data : data})
			})
		}
		],function(err,data) {
		console.log(err)
		console.log(data)
	})
}
//抽奖
handler.lottery = function(msg,session,next) {
	var self = this
	var uid = session.get("uid")
	if(!!uid){
		self.app.rpc.db.award.lottery(session,uid,function(flag){
			next(null,{flag : flag})
		})
	}else{
		next(null,{flag : false})
	}
}
//获取抽奖次数
handler.getLotteryCount = function(msg,session,next) {
	var self = this
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	self.app.rpc.db.award.getLotteryCount(session,uid,function(data){
		next(null,{data : data})
	})
}
//增加抽奖次数
handler.addLotteryCount = function(msg,session,next) {
	var self = this
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	var targetUid = msg.uid
	var count = msg.count
	if(typeof(targetUid) !== "number" || typeof(count) !== "number"){
		cb(flase)
		return
	}
	async.waterfall([
		function(cb) {
			//权限验证
			self.app.rpc.db.remote.getValue(null,uid,"limits",function(data) {
				if(data >= 5){
					cb()
				}else{
					next(null,{flag : false})
				}
			})
		},
		function() {
			self.app.rpc.db.award.addLotteryCount(session,targetUid,count,function(data){
				next(null,{data : data})
			})
		}
		],function(err,data) {
		console.log(err)
		console.log(data)
	})
}
//获取奖励表
handler.getLotteryTable = function(msg,session,next) {
	var self = this
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	self.app.rpc.db.award.getLotteryTable(session,function(data){
		next(null,{data : data})
	})
}
//获取单个玩家奖品背包
handler.getMyLotteryAward = function(msg,session,next) {
	var self = this
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	self.app.rpc.db.award.getLotteryAwardByUid(session,uid,function(data){
		next(null,{data : data})
	})	
}
//获取已获得奖品列表
handler.getLotteryAwardList = function(msg,session,next) {
	var self = this
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	async.waterfall([
		function(cb) {
			//权限验证
			self.app.rpc.db.remote.getValue(null,uid,"limits",function(data) {
				if(data >= 5){
					cb()
				}else{
					next(null,{flag : false})
				}
			})
		},
		function() {
			self.app.rpc.db.award.getLotteryAwardList(session,function(data){
				next(null,{data : data})
			})
		}
		],function(err,data) {
		console.log(err)
		console.log(data)
	})
}
//添加机器人数据
handler.addRobotLottery = function(msg,session,next) {
	var self = this
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	async.waterfall([
		function(cb) {
			//权限验证
			self.app.rpc.db.remote.getValue(null,uid,"limits",function(data) {
				if(data >= 5){
					cb()
				}else{
					next(null,{flag : false})
				}
			})
		},
		function() {
			self.app.rpc.db.award.addRobotLottery(session,msg.index,function(data){
				next(null,{data : data})
			})
		}
		],function(err,data) {
		console.log(err)
		console.log(data)
	})
}
//获取重要物品记录
handler.getImportanceAwardList = function(msg,session,next) {
	var self = this
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	self.app.rpc.db.award.getImportanceAwardList(session,function(data){
		next(null,{data : data})
	})
}
//获取奖品库存
handler.getLotteryInventory = function(msg,session,next) {
	var self = this
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	async.waterfall([
		function(cb) {
			//权限验证
			self.app.rpc.db.remote.getValue(null,uid,"limits",function(data) {
				if(data >= 5){
					cb()
				}else{
					next(null,{flag : false})
				}
			})
		},
		function() {
			self.app.rpc.db.award.getLotteryInventory(session,function(data){
				next(null,{data : data})
			})
		}
		],function(err,data) {
		console.log(err)
		console.log(data)
	})
}
//获取抽奖记录
handler.getLotteryHistory = function(msg,session,next) {
	var self = this
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	async.waterfall([
		function(cb) {
			//权限验证
			self.app.rpc.db.remote.getValue(null,uid,"limits",function(data) {
				if(data >= 5){
					cb()
				}else{
					next(null,{flag : false})
				}
			})
		},
		function() {
			self.app.rpc.db.award.getLotteryHistory(session,function(data){
				next(null,{data : data})
			})
		}
		],function(err,data) {
		console.log(err)
		console.log(data)
	})
}
//申请提现
handler.applyWithdrawal = function(msg,session,next) {
	var uid = session.get("uid")
	var value = msg.value
	var isApp = msg.isApp
	if(!isApp){
		next(null,{flag : false,msg : "use App"})
		return
	}
	if(!uid){
		next(null,{flag : false})
		return
	}
	this.app.rpc.db.withdrawal.applyWithdrawal(null,uid,value,function(flag){
		next(null,{flag : flag})
	})
}
//获取邀请信息
handler.getInviteInfo = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	this.app.rpc.db.award.getInviteInfo(null,uid,function(flag,data){
		next(null,{flag : flag,data : data})
	})
}
//领取邀请宝箱
handler.getInviteAward = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	this.app.rpc.db.award.getInviteAward(null,uid,msg.boxId,function(flag,data){
		next(null,{flag : flag,data : data})
	})
}

//获取app登录信息
handler.getAppLoginInfo = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	this.app.rpc.db.award.getAppLoginInfo(null,uid,function(flag,data){
		next(null,{flag : flag,data : data})
	})
}
//获取app登录奖励
handler.getAppLoginAward = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	this.app.rpc.db.award.getAppLoginAward(null,uid,function(flag,data){
		next(null,{flag : flag,data : data})
	})
}
local.getDateString = function() {
	var myDate = new Date()
	var month = myDate.getMonth() + 1
	var date = myDate.getDate()
	if(month < 10){
		month = "0"+month
	}
	if(date < 10){
		date = "0"+date
	}
	var dateString = parseInt(""+myDate.getFullYear() + month + date)
	return dateString
}
local.getRandomIndex = function(table) {
	var weightAll = 0
	for(var i in table){
		weightAll += table[i]
	}
	//获取奖励
	var tmpRand = Math.random() * weightAll
	var weightCur = 0
	for(var i in table){
		if(tmpRand >= weightCur && tmpRand < table[i] + weightCur){
			return parseInt(i)
		}else{
			weightCur += table[i]
		}
	}
}
//获取新手七日奖信息
handler.getNewPlayerRedInfo = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	this.app.rpc.db.award.getNewPlayerRedInfo(null,uid,function(flag,data){
		next(null,{flag : flag,data : data})
	})
}
//领取每日新手七日奖
handler.getNewPlayerRedDay = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	this.app.rpc.db.award.getNewPlayerRedDay(null,uid,function(flag,data){
		next(null,{flag : flag,data : data})
	})
}
//领取新手七日奖最终红包
handler.getNewPlayerRedAward = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	this.app.rpc.db.award.getNewPlayerRedAward(null,uid,function(flag,data){
		next(null,{flag : flag,data : data})
	})
}

//设置收货地址
handler.setAddress = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	var address = msg.address
	var index = msg.index
	this.app.rpc.db.award.setAddress(null,uid,address,index,function(flag,data){
		next(null,{flag : flag,data : data})
	})
}
//获取收货地址
handler.getAddress = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	this.app.rpc.db.award.getAddress(null,uid,function(flag,data){
		next(null,{flag : flag,data : data})
	})
}
//申请发货
handler.applyDelivery = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	var itemId = msg.itemId
	var index = msg.index
	this.app.rpc.db.award.applyDelivery(null,uid,itemId,index,function(flag,data){
		next(null,{flag : flag,data : data})
	})
}
//玩家获取已发货列表
handler.getUserFinishDeliverys = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	var begin = msg.begin
	if(!begin || typeof(begin) != "number" || parseInt(begin) != begin){
		begin = 1
	}
	this.app.rpc.db.award.getUserFinishDeliverys(null,uid,begin,function(flag,data){
		next(null,{flag : flag,data : data})
	})
}

//玩家获取待发货列表
handler.getUserWaitDeliverys = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false})
		return
	}
	this.app.rpc.db.award.getUserWaitDeliverys(null,uid,function(flag,data){
		next(null,{flag : flag,data : data})
	})
}

